package com.itheima.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.*;

import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishService;
import com.itheima.dto.DishDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service

public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 作用：保存菜品并且携带口味信息
     *
     * @param dishDto 用于接收菜品与口味信息
     * @return
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());

        dishDto.setStatus(1);
        dishDto.setSort(0);

        //保存菜品
        dishMapper.save(dishDto);
        //得到口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().map((flavor -> {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;

        })).collect(Collectors.toList());
        dishFlavorMapper.saveBatch(flavors);

    }
/*    作用： 查询菜品的分页列表
    Params:
    page – 当前页
    pageSize – 页面大小
    name – 菜品的名字
    Returns:*/

    @Override
    public Page<DishDto> page(int page, int pageSize, String name) {
        //1. 设置当前页与页面大小
        PageHelper.startPage(page, pageSize);
        //2. 查询数据，这里得到的是List<Dish>
        List<Dish> dishList = dishMapper.findByName(name);
        //3. 得到PageInfo,pageINfo对象里面存储的是Dish.
        PageInfo<Dish> PageInfo = new PageInfo<>(dishList);

        List<DishDto> dishDtoList = new ArrayList<>();
        //4. 得到分页数据的集合，然后遍历所有的Dish，然后吧每一个Dish转换为DishDTO,并且添加到集合中
        for (Dish dish : PageInfo.getList()) {
            DishDto dishDto = new DishDto();

            //把dish的属性值拷贝到dishDto里面.
            //什么时候可以使用属性拷贝： 两个对象的属性名与类型都一致的情况下，那么就可以拷贝,如果不对应的属性则不拷贝
            BeanUtils.copyProperties(dish, dishDto);
            //根据菜品的类别id查找类别
            Category category = categoryMapper.findById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());

            dishDtoList.add(dishDto);

        }
        Page<DishDto> dishDtoPage = new Page<>(dishDtoList, PageInfo.getTotal(), page, pageSize,PageInfo.getPages());

        return dishDtoPage;

//        5. 自己封装Page对象
    }

    @Override
    public DishDto findById(long id) {
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.findById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());

        dishMapper.updateById(dishDto);
        dishFlavorMapper.deleteFlavoeByDishId(dishDto.getId());

        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            flavor.setUpdateTime(dishDto.getUpdateTime());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setCreateTime(dishDto.getCreateTime());
            flavor.setCreateUser(dishDto.getCreateUser());

        }
////        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
////            //对每一个元素进行加工
////            flavor.setDishId(dishDto.getId());
////            flavor.setCreateTime(LocalDateTime.now());
////            flavor.setUpdateTime(LocalDateTime.now());
////            flavor.setCreateUser(dishDto.getCreateUser());
////            flavor.setUpdateUser(dishDto.getUpdateUser());
////            return flavor;
////        })).collect(Collectors.toList());
        dishFlavorMapper.saveBatch(flavors);
    }

    @Override
    public List<DishDto> findByCategoryId(long categoryId,Integer status) {
       List<Dish> dishList=dishMapper.findByCategoryId(categoryId,status);
       //创建一个空的集合接收disdto
        List<DishDto> dishDtoList =new ArrayList<>();
          //遍历把dish转到dishdto里
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //添加口味去dishdto里
            List<DishFlavor> dishFlavorList = dishFlavorMapper.findById(dish.getId());
            //统计类别去dishdto里
            Category categorylist = categoryMapper.findById(dish.getCategoryId());
            dishDto.setFlavors(dishFlavorList);
            dishDto.setCategoryName(categorylist.getName());
            dishDtoList.add(dishDto);
        }
        return dishDtoList;
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断是否是不是停售状态
        long count =dishMapper.findStatusCount(ids);
        if (count>0){
            throw new ClassCastException("该菜品已启售，不能删除");
        }
//删除菜品
        dishMapper.deleteByIds(ids);
   //删除分类
   dishFlavorMapper.deleteFlavoeByDishIds(ids);
//    categoryMapper.deleteDishByCategoryId(ids);

    }

    /**
     * 改变状态
     * @param status 状态
     * @param ids
     */
    @Override
    public void status(Integer status, List<Long> ids) {
      dishMapper.status(status,ids);
    }
}
